import streamlit as st
import requests
import os
from dotenv import load_dotenv

# 加载环境变量
load_dotenv()
BACKEND_URL = os.getenv("BACKEND_URL", "http://localhost:20000")

# 页面配置
st.set_page_config(
		page_title="法律判决预测系统",
		page_icon="⚖️",
		layout="wide",
)

# 自定义 CSS + JS（支持复制）
st.markdown(
		"""
		<style>
		/* 整个页面居中对齐 */
		.block-container {
			max-width: 900px;
			margin: auto;
			padding-top: 2rem;
		}

		/* 标题居中 */
		.title-center {
			text-align: center;
			font-size: 2.2rem;
			font-weight: bold;
		}

		/* 输入框 placeholder 半透明白色 */
		textarea::placeholder {
			color: rgba(255, 255, 255, 0.5);
		}

		/* 输入框样式，稍微透明 */
		textarea {
			background-color: rgba(255, 255, 255, 0.05);
			color: white;
			border: 1px solid rgba(255, 255, 255, 0.2);
			border-radius: 10px;
			padding: 10px;
			font-size: 1rem;
		}

		/* 复制按钮样式 */
		.copy-btn {
			background-color: rgba(255, 255, 255, 0.1);
			border: 1px solid rgba(255, 255, 255, 0.2);
			color: white;
			padding: 4px 8px;
			border-radius: 5px;
			cursor: pointer;
			font-size: 0.85rem;
			margin-top: 5px;
		}
		.copy-btn:hover {
			background-color: rgba(255, 255, 255, 0.2);
		}
		</style>

		<script>
		function copyText(id) {
			const text = document.getElementById(id).innerText;
			navigator.clipboard.writeText(text).then(() => {
				const btn = document.getElementById(id + "_btn");
				const original = btn.innerText;
				btn.innerText = "✅ 已复制";
				setTimeout(() => btn.innerText = original, 1500);
			});
		}
		</script>
		""",
		unsafe_allow_html=True
)

# 页面标题
st.markdown('<div class="title-center">⚖️ 法律判决智能预测系统</div>', unsafe_allow_html=True)

# 输入区域
case_text = st.text_area(
		"",
		height=220,
		placeholder="输入案情描述，系统将自动预测判决结果、检索相似案例并生成司法解释和判决书。请输入案情描述...",
)

# 预测按钮
if st.button("生成预测结果", type="primary"):
	if not case_text.strip():
		st.error("请输入案情描述后再进行预测")
	else:
		with st.spinner("正在分析案情并生成结果..."):
			try:
				response = requests.post(
						f"{BACKEND_URL}/predict",
						json={"text": case_text}
				)
				response.raise_for_status()
				result = response.json()
				
				# 相似案例
				with st.expander("🔍 相似案例参考", expanded=True):
					similar_text = result.get("similar_cases", "")
					st.markdown(f'<div id="similar_cases">{similar_text}</div>', unsafe_allow_html=True)
					st.markdown(
							'<button id="similar_cases_btn" class="copy-btn" onclick="copyText(\'similar_cases\')">📋 复制内容</button>',
							unsafe_allow_html=True)
				
				# 判决结果预测
				with st.expander("📝 判决结果预测", expanded=True):
					judgment_text = result.get("judgment_result", "")
					st.markdown(f'<div id="judgment_result">{judgment_text}</div>', unsafe_allow_html=True)
					st.markdown(
							'<button id="judgment_result_btn" class="copy-btn" onclick="copyText(\'judgment_result\')">📋 复制内容</button>',
							unsafe_allow_html=True)
				
				# 司法解释
				with st.expander("📚 司法解释", expanded=True):
					interpretation_text = result.get("judicial_interpretation", "")
					st.markdown(f'<div id="judicial_interpretation">{interpretation_text}</div>',
					            unsafe_allow_html=True)
					st.markdown(
							'<button id="judicial_interpretation_btn" class="copy-btn" onclick="copyText(\'judicial_interpretation\')">📋 复制内容</button>',
							unsafe_allow_html=True)
				
				# 判决书
				with st.expander("⚖️ 生成判决书", expanded=True):
					verdict_text = result.get("verdict", "")
					st.markdown(f'<div id="verdict">{verdict_text}</div>', unsafe_allow_html=True)
					st.markdown(
							'<button id="verdict_btn" class="copy-btn" onclick="copyText(\'verdict\')">📋 复制内容</button>',
							unsafe_allow_html=True)
			
			except Exception as e:
				st.error(f"处理失败：{str(e)}")

# 页脚
st.markdown("---")
st.caption("注：本系统仅提供参考，不构成法律意见")
